// pages/pay/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},//地址信息
    cart: [],//购物车数据
    sum: 0,//结算数量
    totals: 0,//总计
    isActive: false,//是否显示奇怪的加载页面
    password: [],//密码
    jianpan: false,//键盘点击样式
  },
  //点击叉
  unJianpan() {
    this.setData({
      jianpan: false
    })
  },
  //点击键盘
  kuang() {
    let now = this.data.password
    now.push('●')
      this.setData({
        password: now
      })
    if (now.length < 5) {
      
    } else {
      this.setData({
        jianpan: false,
        isActive: true
      })
      setTimeout(() => {
        this.setData({
          isActive: false
        })
        wx.navigateTo({
          url: `../finsh/index?totals=${this.data.totals}`,
        });
      }, 1300)
      let carts = wx.getStorageSync('cart')
      let cart = this.data.cart
      let indexs = carts.filter((item, index) => {
        cart.forEach(items=>{
          if(items.goods_id  == item.goods_id){
            return index
          }
        })
      })
      carts.splice(indexs, 1)
      this.setData({
        cart:carts
      })
      wx.getStorageSync('cart', carts)
    }
  },
  //点击删除键
  unKuang() {
    let now = this.data.password
    now.pop()
    this.setData({
      password: now
    })
  },
  //点击微信支付
  wxzf() {
    this.setData({
      isActive: true,
      password: []
    })
    setTimeout(() => {
      this.setData({
        isActive: false,
        jianpan: true
      })
    }, 1500)
  },
  //计算按钮个数计算
  settlement() {
    let sum = this.data.cart.filter(item => item.checked == true)
    this.setData({
      sum: sum.length
    })
  },
  //总计计算
  total() {
    let sums = this.data.cart.filter(item => item.checked == true)
    let sum = sums.reduce((prev, cur, index) => {
      if (cur.goods_price == 0) return prev + 4999
      return prev + cur.goods_price * cur.num
    }, 0)
    this.setData({
      totals: sum
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let address = wx.getStorageSync('address')
    this.setData({
      address,
    })
    //获取本地购物车数据
    let cart = wx.getStorageSync('cart') || []

    cart = cart.filter(item => item.checked == true)
    this.setData({
      cart,
    })
    //计算结算数量
    this.settlement()
    //计算总结
    this.total()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})